const http = require('http');
const url = require('url');
const fs = require('fs');
const path = require('path');

// 模拟的天气数据
const fakeWeatherData = {
    "北京": {
        weather: "晴",
        temperature: 25,
        forecast: [
            { date: "2025-03-21", weather: "多云", temperature: 23 },
            { date: "2025-03-22", weather: "小雨", temperature: 20 },
            { date: "2025-03-23", weather: "晴", temperature: 26 }
        ]
    },
    "上海": {
        weather: "多云",
        temperature: 22,
        forecast: [
            { date: "2025-03-21", weather: "阴", temperature: 21 },
            { date: "2025-03-22", weather: "中雨", temperature: 18 },
            { date: "2025-03-23", weather: "多云", temperature: 22 }
        ]
    },
    "广州": {
        weather: "小雨",
        temperature: 28,
        forecast: [
            { date: "2025-03-21", weather: "大雨", temperature: 26 },
            { date: "2025-03-22", weather: "雷阵雨", temperature: 24 },
            { date: "2025-03-23", weather: "晴", temperature: 29 }
        ]
    }
};

// 定义静态资源目录
const publicDir = path.join(__dirname, 'public');

// 创建 HTTP 服务器
const server = http.createServer((req, res) => {
    const parsedUrl = url.parse(req.url, true);
    const pathname = parsedUrl.pathname;

    if (pathname === '/weather') {
        const city = parsedUrl.query.city;
        if (city && fakeWeatherData[city]) {
            const data = fakeWeatherData[city];
            res.writeHead(200, { 'Content-Type': 'application/json' });
            res.end(JSON.stringify({
                city,
                weather: data.weather,
                temperature: data.temperature
            }));
        } else {
            res.writeHead(200, { 'Content-Type': 'application/json' });
            res.end(JSON.stringify({ error: '未收录该城市' }));
        }
    } else if (pathname === '/forecast') {
        const city = parsedUrl.query.city;
        if (city && fakeWeatherData[city]) {
            const data = fakeWeatherData[city];
            res.writeHead(200, { 'Content-Type': 'application/json' });
            res.end(JSON.stringify({
                city,
                forecast: data.forecast
            }));
        } else {
            res.writeHead(200, { 'Content-Type': 'application/json' });
            res.end(JSON.stringify({ error: '未收录该城市' }));
        }
    } else {
        // 构建请求的静态资源文件路径
        let filePath = path.join(publicDir, pathname === '/' ? 'index.html' : pathname);

        // 获取文件扩展名
        const extname = path.extname(filePath);
        let contentType = 'text/html';
        switch (extname) {
            case '.js':
                contentType = 'text/javascript';
                break;
            case '.css':
                contentType = 'text/css';
                break;
            case '.json':
                contentType = 'application/json';
                break;
            case '.png':
                contentType = 'image/png';
                break;
            case '.jpg':
                contentType = 'image/jpeg';
                break;
            case '.gif':
                contentType = 'image/gif';
                break;
        }

        // 读取文件
        fs.readFile(filePath, (err, data) => {
            if (err) {
                res.writeHead(404, { 'Content-Type': 'application/json' });
                res.end(JSON.stringify({ error: '未找到该资源' }));
            } else {
                // 文件读取成功，返回文件内容
                res.writeHead(200, { 'Content-Type': contentType });
                res.end(data);
            }
        });
    }
});

// 启动服务器
const port = 3000;
server.listen(port, () => {
    console.log(`服务器正在监听端口 ${port}`);
});
    